--[[

	Sida's Auto Carry, Zilean Plugin by Lillgoalie with the help of other members

	
	Features:
	
		- Features:
			- Will ult an team-mate if below shift menu health (Not very usable at the moment)
			- Can farm Ult with W if wanted
			
		- Combo Mode:
			- Uses Q, W, Q combo (If you have enough mana)
			- Uses E on self if Target is out of range.
			- Uses E on Target if Target is in range.
			
		- Auto Harass mode:
			- Uses, Q, W ,Q (If you have enough mana)
		
		- Instant Harass:
			- Uses Q, W, Q combo on a minion that is about to die to poke enemies in range (Always enabled)

	Version 1.0
	- Public Release
	
	Instructions on saving the file:
	- Save the file as:
		- SidasAutoCarryPlugin - Zilean.lua
	
--]]
class 'Plugin'
if myHero.charName ~= "Zilean" then return end

require "Collision"
require "Prodiction"

local Skills, Keys, Items, Data, Jungle, Helper, MyHero, Minions, Crosshair, Orbwalker = AutoCarry.Helper:GetClasses()
local QAble, WAble, EAble, RAble = false, false, false, false
local UltRange = 0

function Plugin:__init()
	Crosshair:SetSkillCrosshairRange(700)
	Skills:DisableAll()
	PrintChat("Loaded AutoCarry: Zilean")
end

function Plugin:OnTick()
	Target = AutoCarry.GetAttackTarget()
	CheckSkill()
	UltRange = Menu.UltA
	UltimateLowHp()
	LetsTimeWarp()

	if Target then
		if Keys.AutoCarry then
		  Combo(Target)
		end
		if Menu.Poke then
		  Poke(Target)
		end
	end
end

function UltimateLowHp()
	if RAble then 
		for i=1, heroManager.iCount do
			local Ally = heroManager:GetHero(i)
			if Ally.team == myHero.team and not Ally.dead then 
				if GetDistance(Ally) <= 700 then
					if (Ally.health < UltRange) then
						CastSpell(_R, Ally)
					end
				end
			end
		end
	end
end

function LetsTimeWarp()
	if Menu.wUlt then
		if RAble then
		
		else
			if myHero.level >= 6 then
				if WAble then CastSpell(_W) end
			end
		end
	end
end
	
function CheckSkill()
	QAble = (myHero:CanUseSpell(_Q) == READY)
	WAble = (myHero:CanUseSpell(_W) == READY)
	EAble = (myHero:CanUseSpell(_E) == READY)
	RAble = (myHero:CanUseSpell(_R) == READY)
end

function CheckMana()
	if QAble or WAble then 
		qMana = GetSpellData(_Q).mana
		wMana = GetSpellData(_W).mana
		
		if myHero.level >= 6 then
			if ((qMana + wMana)+125) > myHero.mana then
				return false
			else
				return true
			end
		else
			if ((qMana + wMana)) > myHero.mana then
				return false
			else
				return true
			end
		end
	end
end


function Poke(Target)
	if isValidTarget(Target) then
		if CheckMana() then
			local Etr = GetDistance(Target) - getHitBoxRadius(Target)/2
			if Etr < 700 then
				if QAble then CastSpell(_Q, Target) end
				if WAble then CastSpell(_W) end
				CastSpell(_Q, Target)
			end
		end 
	end
end

function Combo(Target)
	if CheckMana() then
		if isValidTarget(Target) then
			local Etr = GetDistance(Target) - getHitBoxRadius(Target)/2
			if Etr < 700 then
				if EAble then CastSpell(_E, Target) end
				if QAble then CastSpell(_Q, Target) end
				if WAble then CastSpell(_W) end
				CastSpell(_Q, Target)
				
			else
				if Etr < 1100 then
					if EAble then CastSpell(_E) end
				end
			end
		else
			if GetDistance(Target)<=1200 then
				if EAble and GetDistance(Target)>=700 then CastSpell(_E) end
			end
		end
	end
end

function isValidTarget(e)
  if e and e.valid and e.team ~= myHero.team and not e.dead and e.bTargetable then
    return true
  end
   return false
end

-- Settings and Variables
TriggerKey = 116 -- Key to trigger quote, default F5 (116), nil = always enabled

QManaCost = {70,85,100,115,130}
WManaCost = 50

timebombExplosionRadius = 330
timebombCastRange = 700

useCombo = true
targetMinion = nil
QWcast = false

function OnTick()
	if useCombo then
		-- Reset if minion dies before second q cast/out of mana
		if targetMinion ~= nil and targetMinion.dead == true then
			targetMinion = nil
			QWcast = false
		end
		
		--Cast initial QW if ready
		if player:CanUseSpell(_Q) == READY and player:CanUseSpell(_W) == READY then
			if targetMinion == nil then
				enemyMinions:update()

				for index, minion in pairs(enemyMinions.objects) do
					for i=1, heroManager.iCount, 1 do
						local teammate = heroManager:getHero(i)
					
						if minion.health < getDmg("Q", minion, myHero) and teammate.team ~= player.team and teammate:GetDistance(minion) <= timebombExplosionRadius and player:GetDistance(minion) <= timebombCastRange then
							targetMinion = minion
							break
						end
					end
					
					if targetMinion ~= nil then break end --stop searching once found
				end
			elseif player.mana > (2 * QManaCost[player:GetSpellData(_Q).level] + WManaCost) then -- if you have enough mana to do full combo
				CastSpell(_Q, targetMinion)
				CastSpell(_W)
				QWcast = true
			end
		-- Cast final Q
		elseif QWcast == true then
			CastSpell(_Q, targetMinion)
		end
	end
end


function OnLoad()
	player = GetMyHero()
	
	enemyMinions = minionManager(MINION_ENEMY, 1000, player, MINION_SORT_HEALTH_ASC)
end

Menu = AutoCarry.Plugins:RegisterPlugin(Plugin(), "Zilean")
Menu:addParam("Poke","Harass enemy", SCRIPT_PARAM_ONKEYDOWN, false, string.byte("G"))
Menu:addParam("Poke","Auto Harass", SCRIPT_PARAM_ONOFF, false)
Menu:addParam("UltA","Ult Below",SCRIPT_PARAM_SLICE, 100, 1, 500, 2)
Menu:addParam("wUlt","Farm Ult Using W", SCRIPT_PARAM_ONKEYTOGGLE, false, string.byte("V"))